﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AssetAllocationSystem.Models;
using AssetAllocationSystem.Repositories;
using NHibernate;
using NHibernate.Criterion;

namespace AssetAllocationSystem.Repositories
{
    public class AllocationRepository : RepositoryBase<Allocation>
    {
        private ISession session;
        public AllocationRepository(ISession session)
            : base(session)
        {
            this.session = session;
        }

        //public IList<Allocation> FindByAvailability(bool IsAvailable)
        //{
        //    var allocation = DetachedCriteria
        //        .For<Allocation>()
        //        .Add(Restrictions.Eq("IsAvailable", IsAvailable))
        //        .GetExecutableCriteria(session)
        //        .List<Allocation>();


        //    return allocation;
        //}

        public IList<Allocation> FindByExpectedReleaseDate(DateTime expiryDate)
        {
            var allocations = 
                DetachedCriteria
                .For<Allocation>()
                .Add(Restrictions.Le("ExpectedReleaseDate", expiryDate))
                .GetExecutableCriteria(session)
                .List<Allocation>();

            return allocations;
        }

        public IList<Allocation> FindByActualReleaseDate(DateTime expiryDate)
        {
            var allocations =
                DetachedCriteria
                .For<Allocation>()
                .Add(Restrictions.Le("ActualReleaseDate", expiryDate))
                .GetExecutableCriteria(session)
                .List<Allocation>();

            return allocations;
        }

        public IList<Allocation> FindByAllocationType(string allocationType)
        {
            var allocations = DetachedCriteria
                .For<Allocation>()
                .Add(Restrictions.Eq("AllocationType", allocationType))
                .GetExecutableCriteria(session)
                .List<Allocation>();

            return allocations;
        }
    }
}